/**
 * 代号:甲骨兽进化 2017：厚溥
 * 文件名：BaseDao.java
 * 创建人：jh
 * 日期：2018年4月28日
 * 修改人：
 * 描述：
 */
package com.jh.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.kd.common.base.controller.BaseController;
import org.kd.utils.JsonReturnData;
import org.kd.utils.WebConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jh.model.User;
import com.jh.service.UserService;

/**
 * 用途：业务模块名称
 */
@Controller
@RequestMapping("/loginController")
public class LoginController extends BaseController {
	@Autowired
	UserService userService;
	@RequestMapping("/loginPage")
	public String loginPage() {
		return "loginPage";
	}
	
	@Value("${spring.redis.host}")
	private String host;
	@Value("${spring.redis.password}")
	private String password;
	@Value("${spring.redis.port}")
	private int port;
	@Value("${spring.redis.timeout}")
	private int timeout;
	
	
	@RequestMapping("/loginCheck")
	@ResponseBody
	public String loginCheck(User user, HttpServletRequest httpServletRequest) {
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
		JsonReturnData<String> jsonReturnData = new JsonReturnData<>(WebConstant.ERRORCODE, "登录失败");
		UsernamePasswordToken token = new UsernamePasswordToken(user.getNickName(),user.getPassword());
		try {
			subject.login(token);
			jsonReturnData.setCode(WebConstant.SUCCESSCODE);
			jsonReturnData.setMsg(WebConstant.SUCCESS);
			return responseAPI.getJsonString(jsonReturnData);
		} catch (Exception e) {
			// TODO: handle exception
			return responseAPI.getJsonString(jsonReturnData);
		} 
	}

	@RequestMapping("/loginOut")
	public String loginOut() {
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
		return "loginPage";
	}
	
	/**
	 *  @功能:跳转到页面
	 *  @作者:jh @代号:甲骨兽进化
	 *  @时间:2018年4月29日
	 *  @return  
	 */
	@RequestMapping("/main")
	@RequiresPermissions("layim:init") //标记权限
	public String main(Model model) {
		User param = (User)SecurityUtils.getSubject().getPrincipal();
		model.addAttribute("user",param );
		return "main";
	}
}
